package com.wz.project.api.sap.service;

import com.wz.common.utils.ServletUtils;
import com.wz.project.api.barcode.domain.MatnrInfo;
import com.wz.project.api.tmsMatnr.domain.TmMatnr;
import com.wz.project.api.tmsMatnr.service.ITmMatnrService;
import com.wz.project.bas.basJobId.service.IBasJobIdService;
import com.wz.project.bas.customer.domain.Customer;
import com.wz.project.bas.customer.mapper.CustomerMapper;
import com.wz.project.bas.product.domain.Product;
import com.wz.project.bas.product.mapper.ProductMapper;
import com.wz.project.bas.supplier.domain.Supplier;
import com.wz.project.bas.supplier.mapper.SupplierMapper;
import com.wz.project.inStock.stockin.domain.InStockin;
import com.wz.project.inStock.stockin.mapper.InStockinMapper;
import com.wz.project.inStock.stockindetail.domain.InStockinDetail;
import com.wz.project.inStock.stockindetail.mapper.InStockinDetailMapper;
import com.wz.project.outStock.stockout.domain.OutStockout;
import com.wz.project.outStock.stockout.mapper.OutStockoutMapper;
import com.wz.project.outStock.stockoutdetail.domain.OutStockoutDetail;
import com.wz.project.outStock.stockoutdetail.mapper.OutStockoutDetailMapper;
import com.wz.project.pda.service.PdaService;
import com.wz.project.sap.CUSTOMER.domain.ZflWmsGetCustomer;
import com.wz.project.sap.DN.domain.ZflWmsGetDn;
import com.wz.project.sap.MATERIAL.domain.ZflWmsGetMaterial;
import com.wz.project.sap.PRO_LIST_DNLIST.domain.YppRfcGetWmsProListDnlist;
import com.wz.project.sap.PRO_LIST_PRDLIST.domain.YppRfcGetWmsProListPrdlist;
import com.wz.project.sap.VENDOR.domain.ZflWmsGetVendor;
import com.wz.project.system.user.domain.User;
import com.wz.project.util.StringUtil;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class SapServiceImpl implements SapService {
    @Autowired
    ProductMapper productMapper;
    @Autowired
    SupplierMapper supplierMapper;
    @Autowired
    CustomerMapper customerMapper;
    @Autowired
    InStockinMapper inStockinMapper;
    @Autowired
    IBasJobIdService iBasJobIdService;
    @Autowired
    InStockinDetailMapper inStockinDetailMapper;
    @Autowired
    OutStockoutMapper outStockoutMapper;
    @Autowired
    OutStockoutDetailMapper outStockoutDetailMapper;
    @Autowired
    PdaService pdaService;
    @Autowired
    private ITmMatnrService tmMatnrService;

    @Override
    public void insertProduct(ZflWmsGetMaterial zflWmsGetMaterial) {
        Product product1 = productMapper.selectProductByproductId(zflWmsGetMaterial.getMATNR());
        Product product = new Product();
        product.setProductName(zflWmsGetMaterial.getMAKTX());
        product.setModel(zflWmsGetMaterial.getFERTH());
        product.setParentId(zflWmsGetMaterial.getMATKL());
        product.setSimpleName(zflWmsGetMaterial.getWGBEZ());
        product.setUnit(zflWmsGetMaterial.getMEINS());
        product.setSpec(zflWmsGetMaterial.getMEINS_TXT());
        product.setSpecialFlag(zflWmsGetMaterial.getXCHPF());
        product.setProductFlag(zflWmsGetMaterial.getSERNP());
        product.setMnemonicCode(zflWmsGetMaterial.getSPART());
        product.setUseable("Y");
        if (null == product1) {
            product.setProductGuid(UUID.randomUUID().toString());
            product.setProductId(zflWmsGetMaterial.getMATNR());
            productMapper.insertProduct(product);
        } else {
            product.setProductGuid(product1.getProductGuid());
            productMapper.updateProduct(product);
        }

    }

    @Override
    public void insertSupplier(ZflWmsGetVendor zflWmsGetVendor) {
        Supplier supplier1 = supplierMapper.selectSupplierBySupplierNo(zflWmsGetVendor.getLIFNR());
        Supplier supplier = new Supplier();
        supplier.setSupplierName(zflWmsGetVendor.getNAME1());
        if (supplier1 == null) {
            supplier.setId(UUID.randomUUID().toString());
            supplier.setSupplierNo(zflWmsGetVendor.getLIFNR());
            supplierMapper.insertSupplier(supplier);
        } else {
            supplier.setId(supplier1.getId());
            supplierMapper.updateSupplier(supplier);
        }
    }


    @Override
    public void insertCustomer(ZflWmsGetCustomer zflWmsGetCustomer) {
        Customer customer1 = customerMapper.selectCustomerByCustomerNo(zflWmsGetCustomer.getKUNNR());
        Customer customer = new Customer();
        customer.setCustomerName(zflWmsGetCustomer.getNAME1());
        if (customer1 == null) {
            customer.setId(UUID.randomUUID().toString());
            customer.setCustomerNo(zflWmsGetCustomer.getKUNNR());
            customerMapper.insertCustomer(customer);
        } else {
            customer.setId(customer1.getId());
            customerMapper.updateCustomer(customer);
        }

    }

    @Override
    public void insertInstockin(YppRfcGetWmsProListPrdlist yppRfcGetWmsProListPrdlist) {
        InStockin inStockin1 = inStockinMapper.selectInStockinById(yppRfcGetWmsProListPrdlist.getAUFNR());
        if (inStockin1 == null) {
            InStockin inStockin = new InStockin();
            inStockin.setStockinGuid(yppRfcGetWmsProListPrdlist.getAUFNR());
//            String stockinId = iBasJobIdService.getjobId("RY");
            inStockin.setStockinId(yppRfcGetWmsProListPrdlist.getAUFNR());
            //1生产订单
            //2内向交货单
            inStockin.setInType("1");
            inStockin.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            inStockin.setCustomerId(yppRfcGetWmsProListPrdlist.getMANDT());
//            inStockin.setSupplierId(yppRfcGetWmsProListPrdlist.getWERKS());
            inStockin.setStatus("1");
            //获取当前操作员
//            User user = (User) ServletUtils.getSubject().getPrincipal();
//            String userName = user.getUserName();
            inStockin.setCreateManId("敏华");
            InStockinDetail inStockinDetail = new InStockinDetail();
            inStockinDetail.setStockinDetailId(yppRfcGetWmsProListPrdlist.getAUFNR() + "-1");
            inStockinDetail.setStockinId(yppRfcGetWmsProListPrdlist.getAUFNR());
            inStockinDetail.setStockinGuid(yppRfcGetWmsProListPrdlist.getAUFNR());
            inStockinDetail.setProductId(yppRfcGetWmsProListPrdlist.getMATNR());
            inStockinDetail.setProductGuid(yppRfcGetWmsProListPrdlist.getMAKTX());
            inStockinDetail.setCargoNo(yppRfcGetWmsProListPrdlist.getLGPRO());
            inStockinDetail.setQuantity(new BigDecimal(yppRfcGetWmsProListPrdlist.getPSMNG()));
            inStockinDetail.setAssignNum(BigDecimal.ZERO);
//            inStockinDetail.setPrintDate(yppRfcGetWmsProListPrdlist.getKDAUF());
            inStockinDetail.setRemark(yppRfcGetWmsProListPrdlist.getMEINH());
            inStockinDetail.setStockStatus(yppRfcGetWmsProListPrdlist.getKDAUF() +'-'+StringUtil.Stringdel(yppRfcGetWmsProListPrdlist.getKDPOS()));
//            inStockin.setWarehouseId("01");
            inStockinMapper.insertInStockin(inStockin);
            inStockinDetailMapper.insertInStockinDetail(inStockinDetail);
        } else {
            //加子单
            List<InStockinDetail> inStockinDetail1 = inStockinDetailMapper.selectInstockDetail(yppRfcGetWmsProListPrdlist.getAUFNR(), yppRfcGetWmsProListPrdlist.getMATNR());
//            InStockinDetail inStockinDetail2 = inStockinDetailMapper.selectInstockDetail2(yppRfcGetWmsProListPrdlist.getAUFNR(), yppRfcGetWmsProListPrdlist.getMATNR(),yppRfcGetWmsProListPrdlist.getVGPOS());
//            if(inStockinDetail1.size()>0) {

            int i = inStockinDetailMapper.selectInStockinDetaiCount(yppRfcGetWmsProListPrdlist.getAUFNR());
            InStockinDetail inStockinDetail = new InStockinDetail();
            inStockinDetail.setStockinDetailId(yppRfcGetWmsProListPrdlist.getAUFNR() + "-" + (i + 1));
            inStockinDetail.setStockinId(inStockin1.getStockinId());
            inStockinDetail.setStockinGuid(yppRfcGetWmsProListPrdlist.getAUFNR());
            inStockinDetail.setProductId(yppRfcGetWmsProListPrdlist.getMATNR());
            inStockinDetail.setProductGuid(yppRfcGetWmsProListPrdlist.getMAKTX());
            inStockinDetail.setQuantity(new BigDecimal(yppRfcGetWmsProListPrdlist.getPSMNG()));
            inStockinDetail.setAssignNum(BigDecimal.ZERO);
            inStockinDetail.setCargoNo(yppRfcGetWmsProListPrdlist.getLGPRO());
            inStockinDetail.setRemark(yppRfcGetWmsProListPrdlist.getMEINH());
            inStockinDetail.setPrintDate(yppRfcGetWmsProListPrdlist.getKDAUF());
            inStockinDetail.setStockStatus(yppRfcGetWmsProListPrdlist.getKDAUF() +'-'+StringUtil.Stringdel(yppRfcGetWmsProListPrdlist.getKDPOS()));
            inStockinDetailMapper.insertInStockinDetail(inStockinDetail);
//            }
        }
    }

    @Override
    public void insertInstockinDetail(YppRfcGetWmsProListDnlist yppRfcGetWmsProListDnlist) {
        InStockin inStockin = inStockinMapper.selectInStockinById(yppRfcGetWmsProListDnlist.getAUFNR());
        if (inStockin != null) {
            InStockinDetail inStockinDetail = new InStockinDetail();
            inStockinDetail.setStockinGuid(yppRfcGetWmsProListDnlist.getAUFNR());
            inStockinDetail.setProductId(yppRfcGetWmsProListDnlist.getMATNR());
            inStockinDetail.setRemark(yppRfcGetWmsProListDnlist.getVBELN());
            inStockinDetailMapper.updateInStockinDetailByProduct(inStockinDetail);

            //往子单里加交货单信息


        }

    }

    @Override
    public void insertOutStockout(ZflWmsGetDn zflWmsGetDn,List<MatnrInfo> matnrInfoList) {
        OutStockout outStockout1 = outStockoutMapper.selectOutStockoutById(zflWmsGetDn.getVBELN());

        if (outStockout1 == null) {
            String stockoutId = iBasJobIdService.getjobId("CY");
            OutStockout outStockout = new OutStockout();
            outStockout.setStockoutGuid(zflWmsGetDn.getVBELN());
            outStockout.setStockoutId(zflWmsGetDn.getVBELN());
            outStockout.setCreateManId("敏华");
            outStockout.setOutType("1");
            outStockout.setCustomerNo(zflWmsGetDn.getKUNNR());
            outStockout.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            outStockout.setStatus("1");
            outStockoutMapper.insertOutStockout(outStockout);
            OutStockoutDetail outStockoutDetail2 = outStockoutDetailMapper.selectDetailByPro2(zflWmsGetDn.getVBELN(), zflWmsGetDn.getMATNR(), zflWmsGetDn.getPOSNR());
            if (outStockoutDetail2 == null) {
                for (int i = 0; i < matnrInfoList.size(); i++) {
                    OutStockoutDetail outStockoutDetail = new OutStockoutDetail();
                    outStockoutDetail.setStockoutGuid(zflWmsGetDn.getVBELN());
                    outStockoutDetail.setStockoutId(zflWmsGetDn.getVBELN());
                    outStockoutDetail.setStockoutDetailId(zflWmsGetDn.getVBELN() + "-" + (1 + i));
                    outStockoutDetail.setProductId(matnrInfoList.get(i).getMatnr());
                    outStockoutDetail.setProductGuid(zflWmsGetDn.getMATNR());
                    outStockoutDetail.setAssignNum(BigDecimal.ZERO);
                    outStockoutDetail.setAllotQuantity(BigDecimal.ZERO);
                    outStockoutDetail.setRemark(zflWmsGetDn.getVRKME());
                    outStockoutDetail.setEntryid(zflWmsGetDn.getLGORT());
                    outStockoutDetail.setQuantity(new BigDecimal(zflWmsGetDn.getLFIMG()));
                    outStockoutDetail.setErpStockoutId(zflWmsGetDn.getPOSNR());
                    outStockoutDetailMapper.insertOutStockoutDetail(outStockoutDetail);
                }
            }
        } else {
//            OutStockoutDetail outStockoutDetail1 = outStockoutDetailMapper.selectDetailByPro(zflWmsGetDn.getVBELN(), zflWmsGetDn.getMATNR());
            OutStockoutDetail outStockoutDetail2 = outStockoutDetailMapper.selectDetailByPro2(zflWmsGetDn.getVBELN(), zflWmsGetDn.getMATNR(), zflWmsGetDn.getPOSNR());
            if (outStockoutDetail2 == null) {
                int j = outStockoutDetailMapper.selectOutStockinDetaiCount(zflWmsGetDn.getVBELN());
                for (int i = 0; i < matnrInfoList.size(); i++) {
                    OutStockoutDetail outStockoutDetail = new OutStockoutDetail();
                    outStockoutDetail.setStockoutGuid(zflWmsGetDn.getVBELN());
                    outStockoutDetail.setStockoutId(zflWmsGetDn.getVBELN());
                    outStockoutDetail.setStockoutDetailId(zflWmsGetDn.getVBELN() + "-" + (i + 1 + j));
                    outStockoutDetail.setProductId(matnrInfoList.get(i).getMatnr());
                    outStockoutDetail.setProductGuid(zflWmsGetDn.getMATNR());
                    outStockoutDetail.setAssignNum(BigDecimal.ZERO);
                    outStockoutDetail.setAllotQuantity(BigDecimal.ZERO);
                    outStockoutDetail.setRemark(zflWmsGetDn.getVRKME());
                    outStockoutDetail.setEntryid(zflWmsGetDn.getLGORT());
                    outStockoutDetail.setQuantity(new BigDecimal(zflWmsGetDn.getLFIMG()));
                    outStockoutDetail.setErpStockoutId(zflWmsGetDn.getPOSNR());
                    outStockoutDetailMapper.insertOutStockoutDetail(outStockoutDetail);
                }
            }
        }

    }

    @Override
    public void insertInStockinDN(ZflWmsGetDn zflWmsGetDn, List<MatnrInfo> matnrInfoList) {
        InStockin inStockin1 = inStockinMapper.selectInStockinById(zflWmsGetDn.getVBELN());

        if (inStockin1 == null) {
            InStockin inStockin = new InStockin();
            inStockin.setStockinGuid(zflWmsGetDn.getVBELN());
            inStockin.setCreateManId("敏华");
            inStockin.setStockinId(zflWmsGetDn.getVBELN());
            inStockin.setInType("2");
            inStockin.setSupplierId(zflWmsGetDn.getLIFNR());
            inStockin.setStatus("1");
            inStockin.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            inStockinMapper.insertInStockin(inStockin);
            InStockinDetail inStockinDetail2 = inStockinDetailMapper.selectInstockDetail2(zflWmsGetDn.getVBELN(), zflWmsGetDn.getMATNR(), zflWmsGetDn.getPOSNR());
            if (inStockinDetail2 == null) {
                for (int i = 0; i < matnrInfoList.size(); i++) {
                    TmMatnr tmMatnr = new TmMatnr();
                    tmMatnr.setInStockId(zflWmsGetDn.getVBELN());
                    tmMatnr.setInStockDId(zflWmsGetDn.getPOSNR());
                    tmMatnr.setMatnr(matnrInfoList.get(i).getMatnr());
                    List<TmMatnr> matnrList = tmMatnrService.selectTmMatnrList(tmMatnr);
                    InStockinDetail inStockinDetail = new InStockinDetail();
                    inStockinDetail.setStockinGuid(zflWmsGetDn.getVBELN());
                    inStockinDetail.setStockinId(zflWmsGetDn.getVBELN());
                    inStockinDetail.setStockinDetailId(zflWmsGetDn.getVBELN() + "-" + (i + 1));
                    inStockinDetail.setProductId(matnrInfoList.get(i).getMatnr());
                    inStockinDetail.setProductGuid(zflWmsGetDn.getMATNR());
                    inStockinDetail.setLotNo(new SimpleDateFormat("yyyyMMdd").format(new Date()));
                    inStockinDetail.setCargoNo(zflWmsGetDn.getLGORT());
                    inStockinDetail.setRemark(zflWmsGetDn.getVRKME());
                    inStockinDetail.setQuantity(new BigDecimal(zflWmsGetDn.getLFIMG()));
                    inStockinDetail.setAssignNum(new BigDecimal(matnrList.size()));
                    inStockinDetail.setPrintDate(zflWmsGetDn.getPOSNR());
                    inStockinDetail.setStockStatus(zflWmsGetDn.getVGBEL()+'-'+ StringUtil.Stringdel(zflWmsGetDn.getVGPOS()));
                    inStockinDetailMapper.insertInStockinDetail(inStockinDetail);
                }
            }
        } else {
//            InStockinDetail inStockinDetail1 = inStockinDetailMapper.selectInstockDetail(zflWmsGetDn.getVBELN(), zflWmsGetDn.getMATNR());
            InStockinDetail inStockinDetail2 = inStockinDetailMapper.selectInstockDetail2(zflWmsGetDn.getVBELN(), zflWmsGetDn.getMATNR(), zflWmsGetDn.getPOSNR());
            if (inStockinDetail2 == null) {
                int j = inStockinDetailMapper.selectInStockinDetaiCount(zflWmsGetDn.getVBELN());
                for (int i = 0; i < matnrInfoList.size(); i++) {
                    TmMatnr tmMatnr = new TmMatnr();
                    tmMatnr.setInStockId(zflWmsGetDn.getVBELN());
                    tmMatnr.setOutStockDId(zflWmsGetDn.getPOSNR());
                    tmMatnr.setMatnr(matnrInfoList.get(i).getMatnr());
                    List<TmMatnr> matnrList = tmMatnrService.selectTmMatnrList(tmMatnr);
                    InStockinDetail inStockinDetail = new InStockinDetail();
                    inStockinDetail.setStockinGuid(zflWmsGetDn.getVBELN());
                    inStockinDetail.setStockinId(zflWmsGetDn.getVBELN());
                    inStockinDetail.setStockinDetailId(zflWmsGetDn.getVBELN() + "-" + (i + 1 + j));
                    inStockinDetail.setProductId(matnrInfoList.get(i).getMatnr());
                    inStockinDetail.setProductGuid(zflWmsGetDn.getMATNR());
                    inStockinDetail.setLotNo(new SimpleDateFormat("yyyyMMdd").format(new Date()));
                    inStockinDetail.setCargoNo(zflWmsGetDn.getLGORT());
                    inStockinDetail.setRemark(zflWmsGetDn.getVRKME());
                    inStockinDetail.setQuantity(new BigDecimal(zflWmsGetDn.getLFIMG()));
                    inStockinDetail.setAssignNum(new BigDecimal(matnrList.size()));
                    inStockinDetail.setPrintDate(zflWmsGetDn.getPOSNR());
                    inStockinDetail.setStockStatus(zflWmsGetDn.getVGBEL()+'-'+ StringUtil.Stringdel(zflWmsGetDn.getVGPOS()));
                    inStockinDetailMapper.insertInStockinDetail(inStockinDetail);
                }
            }
        }
    }
}
